package com.yc.txx.servlet;

import com.google.gson.Gson;
import com.yc.txx.entity.Lease;
import com.yc.txx.entity.User;
import com.yc.txx.mapper.LeaseMapper;
import com.yc.txx.util.MyBatisHelper;
import org.apache.ibatis.session.SqlSession;

import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.*;
import java.io.IOException;
import java.util.List;

@WebServlet(name = "MyLeasesServlet", value = "/MyLeasesServlet.s")
public class MyLeasesServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        try(final SqlSession session = MyBatisHelper.getSession()){
            final LeaseMapper mapper = session.getMapper(LeaseMapper.class);
            User myinfo = (User) request.getSession().getAttribute("myinfo");
            final List<Lease> leases = mapper.selectByUid(myinfo.getId());
            final String json = new Gson().toJson(leases);
            response.getWriter().append(json);
        }
    }
}
